from datetime import datetime

from sqlalchemy import Boolean, Column, Integer, String, Enum, DateTime, ForeignKey, Text
from sqlalchemy.orm import relationship

from database import Base
from database.user_db import enum


class Role(Base):
    """角色表"""
    __tablename__ = "roles"
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(length=8), unique=True, index=True)  # 角色名称
    chinese_name = Column(String(length=8), unique=True, index=True)  # 角色中文名
    status = Column(Enum(enum.Status), default=enum.Status.normal)  # delete 删除, normal 正常
    users = relationship("User", back_populates="role")


class User(Base):
    """用户基础表"""
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String(length=32), unique=True, index=True)  # 用户名
    password = Column(String(length=252))  # 密码
    status = Column(Enum(enum.Status), default=enum.Status.normal)  # delete 删除, normal 正常
    job_num = Column(Integer, nullable=True)  # 工号
    gender = Column(Enum(enum.Gender), default=enum.Gender.male)  # 性别
    role_id = Column(Integer, ForeignKey('roles.id'))  # 角色id
    role = relationship("Role", back_populates="users")  # 角色
    add_time = Column(DateTime, default=datetime.now())


